{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3f2cfc0f",
   "metadata": {},
   "outputs": [],
   "source": [
    "deployment = \"gpt4\"\n",
    "model = \"gpt-4\"\n",
    "embedding_deployment=\"embedding\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7b65ca7",
   "metadata": {},
   "source": [
    "# 回顾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b0c2a20f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.embeddings.openai import OpenAIEmbeddings\n",
    "from langchain.vectorstores import Chroma\n",
    "from langchain.chat_models import AzureChatOpenAI\n",
    "# embedding = OpenAIEmbeddings() #如果直接访问OpenAI的 GPT服务 \n",
    "embedding = OpenAIEmbeddings(deployment=\"embedding\") #如果访问Azure的OpenAI的 GPT服务 \n",
    "persist_directory = 'data/'\n",
    "\n",
    "vectordb = Chroma(\n",
    "    collection_name=\"spotmax\",\n",
    "    embedding_function=embedding,\n",
    "    persist_directory=persist_directory\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7cf29e1b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "16\n"
     ]
    }
   ],
   "source": [
    "print(vectordb._collection.count())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "79e3325e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.chains import RetrievalQA\n",
    "from langchain.chat_models import AzureChatOpenAI\n",
    "# from langchain.chat_models import ChatOpenAI #直接访问OpenAI的GPT服务\n",
    "\n",
    "#llm = ChatOpenAI(model_name=\"gpt-4\", temperature=0) #直接访问OpenAI的GPT服务\n",
    "llm = AzureChatOpenAI(deployment_name = deployment, model_name=model, temperature=0.7, max_tokens=1000) #通过Azure的OpenAI服务\n",
    "\n",
    "qa = RetrievalQA.from_chain_type(llm=llm, chain_type=\"stuff\", retriever=vectordb.as_retriever(),return_source_documents=False)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6b281920",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_response(input):\n",
    "    print(input)\n",
    "    result = qa({\"query\": input})\n",
    "    return result[\"result\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "185c9384",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "什么是MaxGroup？\n",
      "MaxGroup是一个可以根据服务的工作模式和运行时特征，对主机集群的组成提出优化建议的系统。它可以估算出优化节省的成本，并在用户确认优化集群方案后，自动实现新集群的构建或原有集群的构成的直接优化。此外，MaxGroup还可以根据工作负载变化模式和业务需求为系统自动伸缩选择合适的实例类型，并且能够与Kubernetes和Consul无缝集成，实现容器环境下的中断预补偿机制。\n"
     ]
    }
   ],
   "source": [
    "print(get_response(\"什么是MaxGroup？\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c68c71f0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running on local URL:  http://127.0.0.1:7860\n",
      "Running on public URL: https://cba0cfbb1ceca06e05.gradio.live\n",
      "\n",
      "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><iframe src=\"https://cba0cfbb1ceca06e05.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "什么是SpotMax\n"
     ]
    }
   ],
   "source": [
    "import gradio as gr\n",
    "def respond(message, chat_history):\n",
    "    bot_message = get_response(message)\n",
    "    chat_history.append((message, bot_message))\n",
    "    return \"\", chat_history\n",
    "\n",
    "with gr.Blocks() as demo:\n",
    "    chatbot = gr.Chatbot(height=300) #just to fit the notebook\n",
    "    msg = gr.Textbox(label=\"Prompt\")\n",
    "    btn = gr.Button(\"Submit\")\n",
    "    clear = gr.ClearButton(components=[msg, chatbot], value=\"Clear console\")\n",
    "\n",
    "    btn.click(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])\n",
    "    msg.submit(respond, inputs=[msg, chatbot], outputs=[msg, chatbot]) #Press enter to submit\n",
    "gr.close_all()\n",
    "demo.launch(share=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae68e807",
   "metadata": {},
   "source": [
    "# 从文本到图片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0e076eae",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import http.client\n",
    "import requests\n",
    "import time\n",
    "\n",
    "def create_image(prompt):\n",
    "  api_base = os.getenv(\"OPENAI_API_BASE\")\n",
    "  api_key = os.getenv(\"OPENAI_API_KEY\")\n",
    "  api_version = api_version = '2022-08-03-preview'\n",
    "\n",
    "  url = \"{}dalle/text-to-image?api-version={}\".format(api_base, api_version)\n",
    "  headers= { \"api-key\": api_key, \"Content-Type\": \"application/json\" }\n",
    "  body = {\n",
    "      \"caption\": prompt,\n",
    "      \"resolution\": \"512x512\"\n",
    "  }\n",
    "  submission = requests.post(url, headers=headers, json=body)\n",
    "  operation_location = submission.headers['Operation-Location']\n",
    "  retry_after = submission.headers['Retry-after']\n",
    "  status = \"\"\n",
    "  while (status != \"Succeeded\"):\n",
    "      time.sleep(int(retry_after))\n",
    "      response = requests.get(operation_location, headers=headers)\n",
    "      status = response.json()['status']\n",
    "  image_url = response.json()['result']['contentUrl']\n",
    "  #display(Image(url=image_url))\n",
    "  return \"\\n![image](\"+image_url+\")\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "229cbcc6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "![image](https://dalleproduse.blob.core.windows.net/private/images/763edabf-c5e2-4ae6-8ec3-599515a7d82d/generated_00.png?se=2023-09-17T05%3A27%3A02Z&sig=HNROKEG%2FJ7SztyswdNTth26gOvl%2F1ednH2poq7WUhwU%3D&ske=2023-09-22T02%3A18%3A23Z&skoid=09ba021e-c417-441c-b203-c81e5dcd7b7f&sks=b&skt=2023-09-15T02%3A18%3A23Z&sktid=33e01921-4d64-4f8c-a055-5bdaffd5e33d&skv=2020-10-02&sp=r&spr=https&sr=b&sv=2020-10-02)\n"
     ]
    }
   ],
   "source": [
    "print (create_image(\"A dog on the street.\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6259befa",
   "metadata": {},
   "source": [
    "# 构建多模态Chatbot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "73d3d329",
   "metadata": {},
   "outputs": [],
   "source": [
    "def query_spotmax_doc(input):\n",
    "    print(input)\n",
    "    result = qa({\"query\": input})\n",
    "    return result[\"result\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ae415d32",
   "metadata": {},
   "outputs": [],
   "source": [
    "# import os\n",
    "# os.environ[\"SERPER_API_KEY\"] = \"\"\n",
    "# https://serper.dev\n",
    "from langchain.utilities import GoogleSerperAPIWrapper\n",
    "def query_web(question):\n",
    "    search = GoogleSerperAPIWrapper()\n",
    "    return search.run(question)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "b51061bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain import PromptTemplate, OpenAI, LLMChain\n",
    "from langchain.chains import LLMRequestsChain\n",
    "from langchain.chat_models import AzureChatOpenAI\n",
    "# from langchain.chat_models import ChatOpenAI #直接访问OpenAI的GPT服务\n",
    "\n",
    "#llm = ChatOpenAI(model_name=\"gpt-4\", temperature=0) #直接访问OpenAI的GPT服务\n",
    "llm = AzureChatOpenAI(deployment_name = deployment, model_name=model, temperature=0.7, max_tokens=1000) #通过Azure的OpenAI服务\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "4755e749",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.agents import initialize_agent, Tool\n",
    "\n",
    "tools = [\n",
    "    Tool(\n",
    "        name = \"Get current info\",func=query_web,\n",
    "        description=\"\"\"only invoke it when you need to answer questions about realtime info.\n",
    "             And the input should be a search query.\"\"\"),\n",
    "    Tool(\n",
    "        name=\"query spotmax doc\", func=query_spotmax_doc,\n",
    "        description=\"\"\"only invoke it when you need to get the info about spotmax/maxgroup/maxarch/maxchaos, \n",
    "        And the input should be the question.\"\"\"),\n",
    "    Tool(\n",
    "        name=\"create an image\", func=create_image,\n",
    "        description=\"\"\"invoke it when you need to create an image. \n",
    "        And the input should be the description of the image.\"\"\")\n",
    "]\n",
    "\n",
    "agent_chain = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", max_iterations=5,handle_parsing_errors=True,verbose=True)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "baa87ad2",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_response(input):\n",
    "    return agent_chain.run(input)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "5defa47e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running on local URL:  http://127.0.0.1:7862\n",
      "\n",
      "To create a public link, set `share=True` in `launch()`.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><iframe src=\"http://127.0.0.1:7862/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3m我需要先获取北京今天的天气信息，然后根据天气情况推荐合适的穿搭，并创建一张穿搭示意图。\n",
      "Action: Get current info\n",
      "Action Input: 今天北京的天气如何\u001b[0m\n",
      "Observation: \u001b[36;1m\u001b[1;3m83°F\u001b[0m\n",
      "Thought:\u001b[32;1m\u001b[1;3m根据获取的信息，北京今天的气温是83°F（约28°C），属于比较热的天气，所以推荐穿轻薄的短袖和短裤，并注意防晒。\n",
      "Action: create an image\n",
      "Action Input: 一张男士穿着轻薄短袖和短裤的穿搭示意图，配上一顶帽子和一副太阳镜，以防晒。\u001b[0m\n",
      "Observation: \u001b[38;5;200m\u001b[1;3m\n",
      "![image](https://dalleproduse.blob.core.windows.net/private/images/0070d40c-9714-4e9e-ae43-0941e39ae023/generated_00.png?se=2023-09-17T05%3A30%3A38Z&sig=hwhvSUAzlSQLw%2Bc8HQIfT1qXFuuUwyKuTp6bwAk2HZo%3D&ske=2023-09-22T03%3A04%3A32Z&skoid=09ba021e-c417-441c-b203-c81e5dcd7b7f&sks=b&skt=2023-09-15T03%3A04%3A32Z&sktid=33e01921-4d64-4f8c-a055-5bdaffd5e33d&skv=2020-10-02&sp=r&spr=https&sr=b&sv=2020-10-02)\u001b[0m\n",
      "Thought:\u001b[32;1m\u001b[1;3m我现在知道最后的答案了。\n",
      "Final Answer: 今天北京的气温比较热，建议你穿轻薄的短袖和短裤，并配上一顶帽子和一副太阳镜，以防晒。以下是我为你创建的穿搭示意图：![image](https://dalleproduse.blob.core.windows.net/private/images/0070d40c-9714-4e9e-ae43-0941e39ae023/generated_00.png?se=2023-09-17T05%3A30%3A38Z&sig=hwhvSUAzlSQLw%2Bc8HQIfT1qXFuuUwyKuTp6bwAk2HZo%3D&ske=2023-09-22T03%3A04%3A32Z&skoid=09ba021e-c417-441c-b203-c81e5dcd7b7f&sks=b&skt=2023-09-15T03%3A04%3A32Z&sktid=33e01921-4d64-4f8c-a055-5bdaffd5e33d&skv=2020-10-02&sp=r&spr=https&sr=b&sv=2020-10-02)\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3m这个问题需要获取当前最新的减脂食物推荐信息。\n",
      "Action: Get current info\n",
      "Action Input: 推荐减脂食物\u001b[0m\n",
      "Observation: \u001b[36;1m\u001b[1;3m1、 各种豆类，糙米、玉米、小米、高粱、荞麦、燕麦、大麦、薏仁等。（混合做杂粮饭很不错哦） · 2、 根茎类植物：紫薯、马铃薯、芋头、地瓜、南瓜、莲藕、荸荠、菱角等。 1. 碳水化合物： 以中式饮食为主的小伙伴们应该常常吃精米白面，这里推荐大家改吃好吃的粗粮饭，既耐饿又控制血糖上升速度，大量纤维还帮助消化减脂，好处多多。 推荐一种我常吃的搭配以及分量。 带饭党/Meal Prep推荐每餐吃生重50克，熟重100克碳水。 10大超级减脂食物 · 蘑菇. 蘑菇属于低GI食物，含有适量的蛋白质和膳食纤维。 · 菠菜. 菠菜富含维生素（A/C/E）、矿物质（铜、镁、铁、钙等）和植物营养素，是超级减脂食物。 此外，多吃一些低热量的蔬菜，比如白菜、芹菜、西兰花、冬瓜、黄瓜、甘蓝等食物，可以补充各种维生素、纤维素，同时促进肠道蠕动，抑制脂肪的堆积。 2、 ... 1. 天然甜點：優格、藍莓、櫻桃 · 2. 膳食纖維+低升糖：燕麥、大麥 · 3. 減脂飲品：綠茶 · 4. 優質蛋白質：雞蛋、鮭魚、豆類 · 5. 好油脂：橄欖油、杏仁、核桃. 堅果富含健康的脂肪、纖維和蛋白質，是具有健康營養素和達到飽足感的絕佳組合！堅果類如杏仁、山核桃和南瓜子，是可以減少腹部脂肪的健康脂肪；有些堅果如 ... ... 减肥减脂来说“吃”更重要可我们通常就会因为太饿 ... Duration: 9:08. Posted: Jul 18, 2021. 一个苹果的热量为50卡路里，但消化和吸收它却需要75卡路里，用它减肥再好不过，同时还能美容，真是一举两得。 ☆木瓜. 有独特的蛋白分解酵素，可以清除因吃肉而积聚在下身 ... 减脂期间建议吃玉米、豌豆、薏米、燕麦、通心粉、红薯、土豆、糙米。这8种食物作为主食十分的健康，饱腹感很强。 01. 玉米. 玉米最适合在减肥期间吃 ... 作为一个资深营养师，今天我就来分享9种强烈的减脂食物。这些食物不仅美味可口，还能助你迅速燃烧脂肪，轻松减脂。 1. 绿茶. 绿茶被誉为减肥 ...\u001b[0m\n",
      "Thought:\u001b[32;1m\u001b[1;3m我现在有了减脂食物的推荐清单\n",
      "Final Answer: 下面是一些推荐的减脂食物：\n",
      "1. 各种豆类，糙米、玉米、小米、高粱、荞麦、燕麦、大麦、薏仁等.\n",
      "2. 根茎类植物：紫薯、马铃薯、芋头、地瓜、南瓜、莲藕、荸荠、菱角等.\n",
      "3. 低热量的蔬菜，比如白菜、芹菜、西兰花、冬瓜、黄瓜、甘蓝等食物.\n",
      "4. 天然甜点：優格、藍莓、櫻桃.\n",
      "5. 膳食纖維+低升糖：燕麥、大麦.\n",
      "6. 減脂飲品：綠茶.\n",
      "7. 優質蛋白質：雞蛋、鮭魚、豆類.\n",
      "8. 好油脂：橄欖油、杏仁、核桃.\n",
      "9. 玉米、豌豆、薏米、燕麦、通心粉、红薯、土豆、糙米.\n",
      "10. 绿茶.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3m这个问题需要我创建一幅图片，但是没有提供具体的食物信息。我需要先从问题中获取更多的信息。\n",
      "Action: Get current info\n",
      "Action Input: 早餐的食物内容\u001b[0m\n",
      "Observation: \u001b[36;1m\u001b[1;3m早餐不仅要有主食(面包、馒头、饼、米饭、粥等)，还要有蛋白质类食物(蛋、奶、肉、豆制品)，以及至少一种蔬菜或水果，如果再加上一小把坚果，那就更完美了。 若早上没有食欲，不要强迫自己吃下大量食物，而应循序渐进。 先从喝水、吃流食开始，比如喝一小碗粥，吃一小碗汤面，或喝点酸奶、豆浆等，慢慢培养早晨胃肠对食物的兴趣。\u001b[0m\n",
      "Thought:\u001b[32;1m\u001b[1;3m我现在知道了早餐应该包含的食物种类，包括主食、蛋白质类食物、蔬菜或水果，以及坚果。我可以用这些信息来创建一幅早餐的图片。\n",
      "Action: create an image\n",
      "Action Input: 一份完美的早餐，包含面包作为主食，蛋和奶作为蛋白质源，搭配一份蔬菜和水果，以及一小把坚果。\u001b[0m\n",
      "Observation: \u001b[38;5;200m\u001b[1;3m\n",
      "![image](https://dalleproduse.blob.core.windows.net/private/images/3fb3270c-3bb7-49a5-a60d-4316dedcce33/generated_00.png?se=2023-09-17T05%3A33%3A23Z&sig=xsOI9xGqFoI8UWt51dHbH7Zi9VuWY3JmX2KVRj1cv0k%3D&ske=2023-09-22T13%3A21%3A34Z&skoid=09ba021e-c417-441c-b203-c81e5dcd7b7f&sks=b&skt=2023-09-15T13%3A21%3A34Z&sktid=33e01921-4d64-4f8c-a055-5bdaffd5e33d&skv=2020-10-02&sp=r&spr=https&sr=b&sv=2020-10-02)\u001b[0m\n",
      "Thought:\u001b[32;1m\u001b[1;3m我现在有了这个早餐的图片。\n",
      "Final Answer: 这是我为你创建的早餐图片，它包含了面包、蛋、奶、蔬菜、水果和一小把坚果。![image](https://dalleproduse.blob.core.windows.net/private/images/3fb3270c-3bb7-49a5-a60d-4316dedcce33/generated_00.png?se=2023-09-17T05%3A33%3A23Z&sig=xsOI9xGqFoI8UWt51dHbH7Zi9VuWY3JmX2KVRj1cv0k%3D&ske=2023-09-22T13%3A21%3A34Z&skoid=09ba021e-c417-441c-b203-c81e5dcd7b7f&sks=b&skt=2023-09-15T13%3A21%3A34Z&sktid=33e01921-4d64-4f8c-a055-5bdaffd5e33d&skv=2020-10-02&sp=r&spr=https&sr=b&sv=2020-10-02)\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3m这个问题并不需要查询信息或者创建图片，因为它只是一个陈述，没有问题需要回答。\n",
      "Final Answer: 好的，记住了，你的名字是蔡超。\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3m这个问题不需要查询任何信息，只需要我回答。\n",
      "Final Answer: 对不起，作为一个人工智能，我没有记忆功能，无法记住你的名字。\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "import gradio as gr\n",
    "def respond(message, chat_history):\n",
    "    bot_message = get_response(message)\n",
    "    chat_history.append((message, bot_message))\n",
    "    return \"\", chat_history\n",
    "\n",
    "with gr.Blocks() as demo:\n",
    "    chatbot = gr.Chatbot(height=500) #just to fit the notebook\n",
    "    msg = gr.Textbox(label=\"Prompt\")\n",
    "    btn = gr.Button(\"Submit\")\n",
    "    clear = gr.ClearButton(components=[msg, chatbot], value=\"Clear console\")\n",
    "\n",
    "    btn.click(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])\n",
    "    msg.submit(respond, inputs=[msg, chatbot], outputs=[msg, chatbot]) #Press enter to submit\n",
    "gr.close_all()\n",
    "demo.launch()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d37e415d",
   "metadata": {},
   "source": [
    "# 给Agent添加记忆"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "9c41748c",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.memory import ConversationBufferWindowMemory\n",
    "from langchain.agents import ZeroShotAgent, Tool, AgentExecutor\n",
    "\n",
    "prefix = \"\"\"Have a conversation with a human, answering the following questions as best you can. You have access to the following tools:\"\"\"\n",
    "suffix = \"\"\"Begin!\"\n",
    "\n",
    "{chat_history}\n",
    "Question: {input}\n",
    "{agent_scratchpad}\"\"\"\n",
    "\n",
    "prompt = ZeroShotAgent.create_prompt(\n",
    "    tools,\n",
    "    prefix=prefix,\n",
    "    suffix=suffix,\n",
    "    input_variables=[\"input\", \"chat_history\", \"agent_scratchpad\"],\n",
    ")\n",
    "memory = ConversationBufferWindowMemory(k=10, memory_key=\"chat_history\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "6f6af5da",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mThought: The user is telling me his name is Cai Chao. There's no need for further action since this isn't a question or a request.\n",
      "Final Answer: 很高兴认识你，蔡超！\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mThought: The human just told me their name is 蔡超. So, I can answer this question directly without invoking any tools.\n",
      "Final Answer: 当然，你的名字是蔡超。\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mThought: To answer this question, I need to get the current weather information of Beijing.\n",
      "Action: Get current info\n",
      "Action Input: Beijing weather today\u001b[0m\n",
      "Observation: \u001b[36;1m\u001b[1;3m84°F\u001b[0m\n",
      "Thought:\u001b[32;1m\u001b[1;3mI now know the final answer.\n",
      "Final Answer: 今天北京的天气是84°F。\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mThought: I need to consider the current temperature in order to give a suitable clothing suggestion.\n",
      "Action: Get current info\n",
      "Action Input: Beijing current weather\u001b[0m\n",
      "Observation: \u001b[36;1m\u001b[1;3m84°F\u001b[0m\n",
      "Thought:\u001b[32;1m\u001b[1;3mBased on the temperature, I can suggest appropriate clothing.\n",
      "Final Answer: 在这种温度下，建议您穿短袖和短裤，并携带一个帽子以防晒。\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mThought: The user is asking for a visual representation of the outfit suggestion. I should create an image.\n",
      "Action: create an image\n",
      "Action Input: A person in shorts, a t-shirt, and a sun hat.\u001b[0m\n",
      "Observation: \u001b[38;5;200m\u001b[1;3m\n",
      "![image](https://dalleproduse.blob.core.windows.net/private/images/85f8a835-56a4-4d6d-a6a4-54c605a87573/generated_00.png?se=2023-09-17T05%3A39%3A48Z&sig=X9UCWm9YcUADOvIQ3QtfrbjBIXMr17bQBsH%2FtvZtRlY%3D&ske=2023-09-22T02%3A18%3A23Z&skoid=09ba021e-c417-441c-b203-c81e5dcd7b7f&sks=b&skt=2023-09-15T02%3A18%3A23Z&sktid=33e01921-4d64-4f8c-a055-5bdaffd5e33d&skv=2020-10-02&sp=r&spr=https&sr=b&sv=2020-10-02)\u001b[0m\n",
      "Thought:\u001b[32;1m\u001b[1;3mI now know the final answer\n",
      "Final Answer: 这就是我为你创建的穿搭示意图。\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mThought: The user is asking for a visual representation of the outfit I suggested. I will need to create an image.\n",
      "Action: create an image\n",
      "Action Input: A person wearing a short-sleeved shirt, shorts, and carrying a hat.\u001b[0m\n",
      "Observation: \u001b[38;5;200m\u001b[1;3m\n",
      "![image](https://dalleproduse.blob.core.windows.net/private/images/ddc6427d-f9a3-4330-9a79-4007f698be32/generated_00.png?se=2023-09-17T05%3A40%3A29Z&sig=A%2Br82vALnshvF493qFN13OGd9Pmx%2BcCEi5QoozQMdEs%3D&ske=2023-09-22T13%3A21%3A34Z&skoid=09ba021e-c417-441c-b203-c81e5dcd7b7f&sks=b&skt=2023-09-15T13%3A21%3A34Z&sktid=33e01921-4d64-4f8c-a055-5bdaffd5e33d&skv=2020-10-02&sp=r&spr=https&sr=b&sv=2020-10-02)\u001b[0m\n",
      "Thought:\u001b[32;1m\u001b[1;3mI now have the final image.\n",
      "Final Answer: 这就是我为你创建的穿搭示意图。\n",
      "![image](https://dalleproduse.blob.core.windows.net/private/images/ddc6427d-f9a3-4330-9a79-4007f698be32/generated_00.png?se=2023-09-17T05%3A40%3A29Z&sig=A%2Br82vALnshvF493qFN13OGd9Pmx%2BcCEi5QoozQMdEs%3D&ske=2023-09-22T13%3A21%3A34Z&skoid=09ba021e-c417-441c-b203-c81e5dcd7b7f&sks=b&skt=2023-09-15T13%3A21%3A34Z&sktid=33e01921-4d64-4f8c-a055-5bdaffd5e33d&skv=2020-10-02&sp=r&spr=https&sr=b&sv=2020-10-02)\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "llm_chain = LLMChain(llm=llm, prompt=prompt)\n",
    "agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True, handle_parsing_errors=True)\n",
    "agent_chain = AgentExecutor.from_agent_and_tools(\n",
    "    agent=agent, tools=tools, verbose=True, memory=memory, handle_parsing_errors=True\n",
    ")\n",
    "\n",
    "def get_response(input):\n",
    "    return agent_chain.run(input)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad6e0a45",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5d3c54ca",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
